package com.ydx.demo.Dao;

import com.ydx.demo.entity.Medicine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

//业务逻辑层
@Repository
public class MedicineRepository {
      @Autowired
    private JdbcTemplate JDBCTemplate;

      //添加药品sql语句
    public int addMedicine(Medicine medicine){
        return JDBCTemplate.update("insert into medicine(drugname,price,drugdetail) values(?,?,?)",
                medicine.getDrugname(),medicine.getPrice(),medicine.getDrugdetail());
    }
      //删除学生
    public int deleteMedicine(int id){
        return  JDBCTemplate.update("delete from medicine where id = ?",id);
    }
    //修改药品
    public int updateMedicine(Medicine medicine){
        return JDBCTemplate.update("update medicine set drugname = ?,price = ?,drugdetail= ? where id = 3",
        medicine.getDrugname(),medicine.getPrice(),medicine.getDrugdetail(),medicine.getId());
    }
    //查询所有药品
    public List<Medicine> findAllMedicines(){
        return JDBCTemplate.query("select * from medicine", new BeanPropertyRowMapper<>(Medicine.class));
    }
    //根据id号查询单个药品
    public Medicine findMedicineById(int id){
        return JDBCTemplate.queryForObject("select * from medicine where id = ?",
            new BeanPropertyRowMapper<>(Medicine.class), id);
    }


}
